package com.example.intenttrial;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HandlerAndThreadActivity extends Activity{

	private TrialApplication app;
	
	private TextView text;
	private Button btnThread;
	private Button btnHandler;
	private Button btnHandlerDelayed;
	private Button btnAsyncTask;
	private Button btnAsyncTaskDelayed;
	private Button btnClear;
	
	private int counter = 0;
	private String message = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.screen_handler_and_thread);
		mapUI();
		setListeners();
		
		app = (TrialApplication) getApplication();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	private void mapUI() {
		text = (TextView) findViewById(R.id.tv_messages);
		btnThread = (Button) findViewById(R.id.btn_thread);
		btnHandler = (Button) findViewById(R.id.btn_handler);
		btnHandlerDelayed = (Button) findViewById(R.id.btn_handler_delayed);
		btnAsyncTask = (Button) findViewById(R.id.btn_asynctask);
		btnAsyncTaskDelayed = (Button) findViewById(R.id.btn_asynctask_delayed);
		btnClear = (Button) findViewById(R.id.btn_clear);
	}
	
	private void setListeners() {
		btnHandler.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				addMessage();
			}
		});
		
		btnHandlerDelayed.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				addMessageDelayed();
			}
		});
		
		btnAsyncTask.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TasksFactory.getSimpleTask(app, text).execute();
			}
		});
		
		btnAsyncTaskDelayed.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TasksFactory.getTaskWithTimer(app, text).execute();
			}
		});
		
		btnClear.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				message = "";
				app.clearText();
				text.setText(message);
			}
		});
	}
	
	Handler handler = new Handler(new SimpleCallback());
	Handler handlerWithCallback = new Handler();
	
	private void addMessage() {
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				message += "Message " + ++counter + "\n";
				text.setText(message);
			}
		});
		handler.sendEmptyMessage(1);
	}
	
	private void addMessageDelayed() {
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				message += "Message delayed " + ++counter + "\n";
				text.setText(message);
			}
		}, 2000);
	}
	
	private class SimpleCallback implements Handler.Callback {

		@Override
		public boolean handleMessage(Message msg) {
			message += "Message handled " + ++counter + "\n";
			text.setText(message);
			return false;
		}
	} 
	
}
